#include "Statsheet.h"
Statsheet::Statsheet()
{
	for(int i = 0; i < STAT_NUMOFSTATS; ++i)
	{
		Stats[i] = 0;
	}
}

Statsheet::~Statsheet()
{
}

void Statsheet::LoadStats(char* s_Statsheet, short &p_race)
{
	string srace;
	string trash;
	string scurrskill;
	ifstream TheFile(s_Statsheet);
	if(TheFile.is_open())
	{
		while(!TheFile.eof())
		{
			getline(TheFile, srace,';');
			getline(TheFile, trash,'\n');

			int num = atoi(srace.c_str());//istringstream raceiss(srace);
			p_race = num;
			for(short i = 0; i < STAT_NUMOFSTATS; i++)
			{	
				getline(TheFile, scurrskill, ';');
				num = atoi(scurrskill.c_str());//istringstream skilliss(scurrskill);
				if(i % 5 != 0)
					Stats[i] = num;//skilliss >> this->Stats[i];
				else
					Bonuses[i/5] = num;//skilliss >> Bonuses[i/5];
				getline(TheFile, trash,'\n');
			}
		}
	}
	CalcQualities();
}
void Statsheet::SaveStats(Statsheet p_Stats,  char* s_Statsheet, short race)
{
	std::ofstream out(s_Statsheet);
	if(out.is_open())
	{
		out << race << ';' <<"		Race" << '\n';
		out <<  p_Stats.Bonuses[0]	<< ';' << "		-----Upper Body Bonus" << '\n';
			out << p_Stats.Stats[STAT_REND]				<< ';' << "		Rend" << '\n';
			out << p_Stats.Stats[STAT_BRAWL]			<< ';' << "		Brawl" << '\n';
			out << p_Stats.Stats[STAT_CLIMB]			<< ';' << "		Climb" << '\n';
			out << p_Stats.Stats[STAT_GRAPPLE]			<< ';' << "		Grapple" << '\n';

		out << p_Stats.Bonuses[1]	<< ';' << "		-----Lower Body Bonus" << '\n';
			out << p_Stats.Stats[STAT_SWIM]				<< ';' << "		Swim" << '\n';
			out << p_Stats.Stats[STAT_VAULT]			<< ';' << "		Vault" << '\n';
			out << p_Stats.Stats[STAT_SPRINT]			<< ';' << "		Sprint" << '\n';
			out << p_Stats.Stats[STAT_STOMP]			<< ';' << "		Stomp" << '\n';

		out <<  p_Stats.Bonuses[2]	<< ';' << "		-----Vitality Bonus" << '\n';
			out << p_Stats.Stats[STAT_ARMOR]			<< ';' << "		Armor" << '\n';
			out << p_Stats.Stats[STAT_SURVIVAL]			<< ';' << "		Survival" << '\n';
			out << p_Stats.Stats[STAT_TOUGHNESS]		<< ';' << "		Toughness" << '\n';
			out << p_Stats.Stats[STAT_RESILIENCE]		<< ';' << "		Resilience" << '\n';

		out << p_Stats.Bonuses[3]	<< ';' << "		-----Coordination Bonus" << '\n';
			out << p_Stats.Stats[STAT_STEALTH]			<< ';' << "		Stealth" << '\n';
			out << p_Stats.Stats[STAT_EVASION]			<< ';' << "		Evasion" << '\n';
			out << p_Stats.Stats[STAT_PRECISION]		<< ';' << "		Precision" << '\n';
			out << p_Stats.Stats[STAT_WEAPONRY]			<< ';' << "		Weaponry" << '\n';

		out << p_Stats.Bonuses[4]	<< ';' << "		-----Intelligence Bonus" << '\n';
			out << p_Stats.Stats[STAT_BIOLOGY]			<< ';' << "		Biology" << '\n';
			out << p_Stats.Stats[STAT_GEOLOGY]			<< ';' << "		Geology" << '\n';
			out << p_Stats.Stats[STAT_LINGUISTICS]		<< ';' << "		Linguistics" << '\n';
			out << p_Stats.Stats[STAT_ENGINEERING]		<< ';' << "		Engineering" << '\n';

		out << p_Stats.Bonuses[5]	<< ';' << "		-----Metaphysics Bonus" << '\n';
			out << p_Stats.Stats[STAT_DIVINATION]		<< ';' << "		Divination" << '\n';
			out << p_Stats.Stats[STAT_IMMOLATION]		<< ';' << "		Immolation" << '\n';
			out << p_Stats.Stats[STAT_COMBINATION]		<< ';' << "		Combination" << '\n';
			out << p_Stats.Stats[STAT_MANIPULATION]		<< ';' << "		Manipulation" << '\n';

		out << p_Stats.Bonuses[6]	<< ';' << "		-----Presence Bonus" << '\n';
			out << p_Stats.Stats[STAT_MUSIC]			<< ';' << "		Music" << '\n';
			out << p_Stats.Stats[STAT_CRAFT]			<< ';' << "		Craft" << '\n';
			out << p_Stats.Stats[STAT_CHARM]			<< ';' << "		Charm" << '\n';
			out << p_Stats.Stats[STAT_COOKING]			<< ';' << "		Cooking";
	}
}
bool Statsheet::UpdateStats()
{
	return true;
}
// Begin the functions used for updating an entities aspects
void Statsheet::CalcThresholds(short& a_P, short& a_I, short& a_E)
{
	/*Definition
	A threshold is how much damage an entity can suffer among
	pierce, impact and energy without dying. 

	All are set at base 10 so that an entity can't die from 
	being sneezed on, although 10 is still very low, akin to a 
	simple stab, punch or burn.

	pierceThreshold is bolstered by Survival, because Survival
	ensures that the entity can fight the effects of bleedout

	impactThreshold is bolstered by Toughness, because Toughness
	ensures that the entity can ignore the effects of broken limbs

	energyThreshold is bolstered by Resilience, because resilience
	ensures that the entity can ignore the effects of magic and nature

	A threshold is 10 + (5 * Vitality) + (RelatedSkill/5)
	-Max 80 thresholds if cap is 100
	-130 if cap is 150
	-150 if cap is 200

	Note: Keep in mind that this is when the player dies, not their maximum
	defense, so it being high is not particularly game breaking. Having 
	a pierceResist of 100 is a different matter, though.
	*/

	a_P = 10 + (5 * Stats[STAT_VIT]) + (Stats[STAT_SURVIVAL]   /5);
	a_I = 10 + (5 * Stats[STAT_VIT]) + (Stats[STAT_TOUGHNESS]  /5);
	a_E = 10 + (5 * Stats[STAT_VIT]) + (Stats[STAT_RESILIENCE] /5);
}
short Statsheet::CalcMaxMeta()
{
	/*Definition
	Designer still not sure how to perfect this number, it's going to be 
	a lot of trial and error.
	*/
	return Stats[STAT_MTP] * 5;
}
void Statsheet::CalcNaturalResistances(short& a_P, short& a_I, short& a_E)
{
	/*Definition

	These are the entity's natural resistances to pierce, impact
	and energy damage types. Unlike thresholds, there is no base number
	for a resistance.

	A resistance negates X amount of damage of its type, so someone with
	25 pierce resistance will only accrue 5 pierce damage if struck with
	a force that does 30 pierce damage.

	The calculation for a resistance is relatedStat/4, giving a player up 
	to 25 resistance.  Equivalent roughly to 

	*/

	a_P = Stats[STAT_SURVIVAL]   /5; 
	a_I = Stats[STAT_TOUGHNESS]  /5;
	a_E = Stats[STAT_RESILIENCE] /5;
}
void Statsheet::CalcPull(short& a_Pull)
{
	/*Definition
	Pull is used to determine the strength of bows that the player
	can notch, later used for boltCannons and certain rifles.

	Determined by Rend * UBS
	*/
	a_Pull = Stats[STAT_REND] * Stats[STAT_UBS];
}
void Statsheet::CalcLoads(short& a_ML, short& a_LL)
{
	/*Definiton
	Below a medium load, the entity's 
	acceleration and max speed remain intact
	above it and below large load, the entity
	has halved acceleration and max speed	

	A player can carry 25 pounds for every point
	of Lower and Upper Body Strengths, that is their
	medium load.

	A large load is merely twice the medium load, but once
	achieved the player's max speed and acceleration drop
	to 10%
	*/
	a_ML = (Stats[STAT_UBS] * 25) + Stats[STAT_LBS] * 25;
	a_LL = a_ML * 2;
}
void Statsheet::CalcAttSpeed(short& a_AttSpeed, short skill)
{
	Stats[skill] > 10 ? a_AttSpeed = Stats[skill]/10:a_AttSpeed = 1;
	a_AttSpeed = 1000/a_AttSpeed;
}
void Statsheet::CalcQualities()
{
	/*All qualities are the average of their skills divided by 10*/
	/*Not sure if this will work*/
	for(int i = 0; i < STAT_NUMOFSTATS; i++)
	{
		if(i % 5 == 0)	
			Stats[i] = (Stats[i+1] + Stats[i+2] + Stats[i+3] + Stats[i+4])/40 + Bonuses[i/5];
	}
	
}
void Statsheet::CalcSpeedAttributes(double& a_Acceleration, double& a_MaxSpeed)
{
	//The tweaks prevent 0 movement at level 0
	a_Acceleration = (Stats[STAT_SPRINT]/10) + 1;
	a_MaxSpeed		= (short)(a_Acceleration*1.75f);
}